package leetcode;

/***
 *
 * 141
 * 单向链表中是否存在环:快慢指针
 */
public class LinkedListCycle141 {
    class ListNode {
     int val;
      ListNode next;
      ListNode(int x) {
          val = x;
          next = null;
      }
 }
    public boolean hasCycle(ListNode head) {
        if (head == null || head.next == null){return false;}
        ListNode fast = head,slow = head;
        while (fast !=null && fast.next != null && slow != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(fast == slow){
                return true;
            }
        }
        return false;
    }
}
